package org.pro.View;

import javax.swing.*;
import java.awt.*;
import java.awt.image.ImageObserver;
import java.util.Iterator;
import org.pro.Model.*;

public class GamePanel extends JPanel implements Tank.DrawInterface, Shot.DrawInterface,River.DrawInterface,Gross.DrawInterface{
    private WarData warData;
    private double frameRate = 0.0;
    private GameView gameView; // 添加 GameView 引用
    public GamePanel() {
        setFocusable(true);
        setDoubleBuffered(true); // 启用
        setFocusable(true); // 确保面板可聚焦
    }

    public void setWarData(WarData warData) {
        this.warData = warData;
    }

    public void setFrameRate(double frameRate) {
        this.frameRate = frameRate;
    }

    public void setGameView(GameView gameView) {
        this.gameView = gameView;
    }
    //有的画，有的不画
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;
        if (this.warData != null && this.warData.elements.size() > 0) {
            Iterator var3 = this.warData.elements.iterator();

            while(var3.hasNext()) {
                Element element = (Element)var3.next();
                if (element instanceof Tank) {
                    ((Tank) element).draw(g2,this,warData.getGrossList());
                }
                else if(element instanceof Shot) {
                    ((Shot) element).draw(g2,this,warData.getGrossList());
                }
                else
                    element.draw(g2, this);
            }
        }

        //显示帧率
        String str = String.format("fps:%.2f", this.frameRate);
        g.drawString(str, 10, 15);
        //时间
        // 修改时间显示部分
        String timeStr = String.format("游戏时间: %d 秒", warData.getElapsedTime() / 1000);

        g.drawString(timeStr, 10, 30);
        //显示催婚的坦克数目
        int dematank=this.warData.getdestroytanks();
        String tanksStr=String.format("击败坦克数: %d 个", dematank);
        g.drawString(tanksStr, 10, 50);
    }

    public void draw(Tank refTank, Graphics2D g2) {
        Image img1 = null;
        Image img2 = null;
        if (refTank.team == TankTeam.RED.ordinal()) {
            img1 = ImageCache.get("tank_red");
            img2 = ImageCache.get("turret_red");
        }

        if (refTank.team == TankTeam.BLUE.ordinal()) {
            img1 = ImageCache.get("tank_blue");
            img2 = ImageCache.get("turret_blue");
        }

        Graphics2D g = (Graphics2D)g2.create();
        g.translate(refTank.x, refTank.y);
        g.rotate(Math.toRadians(refTank.dir));
        g.drawImage(img1, -18, -19, (ImageObserver)null);
        g.rotate(Math.toRadians(-refTank.dir));
        g.drawRect(-22, -34, 44, 8);
        g.setColor(Color.RED);
        int whp = (int)(43.08 * (refTank.hp / refTank.hpmax));
        g.fillRect(-21, -33, whp, 7);
        g.rotate(Math.toRadians(refTank.turretDir));
        g.drawImage(img2, -32, -32, (ImageObserver)null);
    }
    //绘制子弹的咯
    public void draw(Shot refShot, Graphics2D g2) {
        Image img = null;
        //如果是红队就是我
        if(refShot.tank.team==TankTeam.RED.ordinal()) {
            img=ImageCache.get("my_shot");
        }
        //如果是蓝队，也就是敌人
        if(refShot.tank.team==TankTeam.BLUE.ordinal()) {
            img=ImageCache.get("shot");
        }
        Graphics2D g = (Graphics2D)g2.create();
        g.translate(refShot.x, refShot.y);
        g.drawImage(img, -6, -6, (ImageObserver)null);
    }

    public void draw(River refRiver, Graphics2D g2) {
        Image img =ImageCache.get("river");
        Graphics2D g = (Graphics2D)g2.create();
        g.translate(refRiver.x, refRiver.y);
        g.drawImage(img, -21, -17, (ImageObserver)null);
    }

    public void draw(Gross refGross,Graphics2D g2) {
        Image img =ImageCache.get("gross");
        Graphics2D g = (Graphics2D)g2.create();
        g.translate(refGross.x, refGross.y);
        g.drawImage(img, -24, -16, (ImageObserver)null);

    }
}
